package com.newblog2_0.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newblog2_0.common.ResultAjax;
import com.newblog2_0.common.SessionUtils;
import com.newblog2_0.mapper.ArticleMapper;
import com.newblog2_0.model.ArticleInfo;
import com.newblog2_0.model.UserInfo;
import com.newblog2_0.model.vo.ArticleInfoVo;
import com.newblog2_0.model.vo.UserInfoVo;
import com.newblog2_0.service.ArticleService;
import com.newblog2_0.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2023-12-16
 * Time:15:18
 * Text：
 */
@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleMapper articleMapper;
    @Resource
    private ThreadPoolTaskExecutor taskExecutor;



//    博客列表
    @RequestMapping("/index")
    public ResultAjax index(Integer pindex, Integer psize, Integer ceo, HttpServletRequest req) throws ExecutionException, InterruptedException {
       if(ceo==1){
           UserInfo userInfo= SessionUtils.getUser(req);
           if(userInfo==null){
               return ResultAjax.fail(-1,"请先登录");
           }
       }
        //加工参数
        if(pindex == null || pindex<1){
            pindex =1;
        }
        if(psize == null ||psize<1){
            psize=2;
        }

        Integer finalPsize = psize;
        Integer finalPindex = pindex;
        //执行当前页的数据
        FutureTask<List<ArticleInfoVo>> ArtListTask=new FutureTask<>(()->{
            int finaloffest = finalPsize *(finalPindex -1);
            return articleService.getListByPage(finalPsize,finaloffest);
        });
        //返回数据的总数，计算出页数。
        FutureTask<Integer> sizeTask=new FutureTask<>(()->{
            int total =articleService.getCount();
            double sizeTemp=(total * 1.0)/(finalPsize * 1.0);
            return (int)Math.ceil(sizeTemp); //向上取整
        });
        FutureTask<List<UserInfo>> UserListTask=new FutureTask<>(()->{
            return  userService.getUserAll();
        });

        //提交给线程池
        taskExecutor.submit(ArtListTask);
        taskExecutor.submit(sizeTask);
        taskExecutor.submit(UserListTask);
        //线程池执行结束返回数据
        List<ArticleInfoVo> art= ArtListTask.get();
        int num=sizeTask.get();
        List<UserInfo> user= UserListTask.get();

        for(ArticleInfoVo art1:art){
            for(UserInfo user1:user){
                if(art1.getUId()==user1.getUId())
                {
                    art1.setName(user1.getUName());
                    break;
                }
            }
        }
        System.out.println(art);
        HashMap<String,Object> t1=new HashMap<>();
        t1.put("num",num);
        t1.put("date",art);
        return ResultAjax.succ(t1);
    }

//    单博客详情页
    @RequestMapping("/getlist")
    public ResultAjax getlist(Integer aId,  HttpServletRequest req) throws ExecutionException, InterruptedException {
        UserInfo userInfo = SessionUtils.getUser(req);
        if (userInfo == null) {
            return ResultAjax.fail(-1, "请先登录");
        }

        ArticleInfo art=articleService.getId(aId);
        UserInfoVo user=userService.getUser(art.getUId());

        int num=articleService.getArtCount(user.getUId());
        user.setCount(num);
        HashMap<String,Object> map=new HashMap<>();
        map.put("art",art);
        map.put("user",user);
        return ResultAjax.succ(map);
    }
//阅读量自增
    @RequestMapping("/increment")
    public ResultAjax increment(Integer id, HttpServletRequest req){
        UserInfo userInfo=SessionUtils.getUser(req);
        if(userInfo==null){
            return ResultAjax.fail(-1,"请先登录");
        }
        int num=articleService.getinsert(id);
        if(num==1){
            return ResultAjax.succ("阅读成功");
        }
        return ResultAjax.fail(-1,"失败");
    }

//添加文章
    @PostMapping("/add")
    public ResultAjax add(ArticleInfo articleInfo,HttpServletRequest req){
        UserInfo userInfo=SessionUtils.getUser(req);
        if(userInfo==null){
            return ResultAjax.fail(-1,"请先登录");
        }
        if(articleInfo==null || !StringUtils.hasLength(articleInfo.getTitle()) ||
                !StringUtils.hasLength(articleInfo.getContent())){
            return ResultAjax.fail(-1,"非法参数");
        }
        UserInfo user=SessionUtils.getUser(req);
        articleInfo.setUId(user.getUId());
        if(articleService.save(articleInfo)){
            return ResultAjax.succ(1);
        }
       return ResultAjax.fail(-1,"后端出现问题");
    }
//个人主页
    @PostMapping("/getMylist")
    public ResultAjax getMylist(HttpServletRequest req){
        UserInfo userInfo=SessionUtils.getUser(req);
        if(userInfo==null){
            return ResultAjax.fail(-1,"请先登陆");
        }
        //获取个人信息
        UserInfoVo user=userService.getUser(userInfo.getUId());
        int num=articleService.getArtCount(userInfo.getUId());
        user.setCount(num);
        //获取文章信息
        List<ArticleInfo> t1=articleMapper.selectList(new QueryWrapper<ArticleInfo>().eq("uId",user.getUId()));
        HashMap<String,Object> map=new HashMap<>();
        map.put("user",user);
        map.put("art",t1);
        return ResultAjax.succ(200,map);
    }

//    删除指定文章
    @GetMapping("/delete")
    public ResultAjax delete(Integer aid){
        if(articleService.remove(new QueryWrapper<ArticleInfo>().eq("aId",aid))){
            return ResultAjax.succ(200,1);
        }
        return ResultAjax.fail(-1,"删除失败");
    }

}
